use std::{env, process::Output};

use log::{error, info};
use my_util_lib::file_util;

use crate::sys::global::{to_global_result, GlobalResult};

/// @Author: DengLibin
/// @Date: Create in 2024-08-05 10:27:01
/// @Description: cmd

pub fn check_output(output: Output) -> GlobalResult<()> {
    let os = env::consts::OS.to_lowercase();
    //info!("操作系统:{}", os);
    let mut decoded_string = String::from_utf8_lossy(&output.stdout).into_owned();
    if os == "windows" {
        // info!("windows输出:{}", decoded_string);
        // 使用 GBK 编码将字节转换为  字符串
        decoded_string = to_global_result(file_util::get_gbk_str(&output.stdout))?;
    }
    // 打印标准输出
    info!("cmd输出: {}", decoded_string);

    // 打印标准错误
    if !output.stderr.is_empty() {
        decoded_string = String::from_utf8_lossy(&output.stderr).into_owned();
        if os == "windows" {
            decoded_string = to_global_result(file_util::get_gbk_str(&output.stderr))?;
        }
        error!("cmd错误: {}", decoded_string);
        // return Err(GlobalError::new(decoded_string));
        return Err(anyhow::anyhow!(decoded_string));

    }
    Ok(())
}
